package cn.iocoder.yudao.module.member.address.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.region.RegionUtils;
import cn.iocoder.yudao.framework.region.enums.RegionDataSet;
import cn.iocoder.yudao.framework.validation.groups.CreateGroup;
import cn.iocoder.yudao.framework.validation.groups.UpdateGroup;
import cn.iocoder.yudao.module.api.member.address.dto.MemberAddressRespDTO;
import cn.iocoder.yudao.module.member.address.dal.dataobject.MemberAddressDO;
import cn.iocoder.yudao.module.member.address.service.AddressService;
import cn.iocoder.yudao.module.member.address.vo.AppAddressSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户地址")
@RestController
@RequestMapping("/member/address")
@Validated
public class AppAddressController {

    @Resource
    private AddressService addressService;

    @PostMapping("/create")
    @Operation(summary = "创建用户地址")
    public CommonResult<Long> createAddress(@Validated(value = CreateGroup.class) @RequestBody AppAddressSaveReqVO reqVO) {
        return success(addressService.createAddress(getLoginUserId(), reqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新用户地址")
    public CommonResult<Boolean> updateAddress(@Validated(value = UpdateGroup.class) @RequestBody AppAddressSaveReqVO reqVO) {
        addressService.updateAddress(getLoginUserId(), reqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除用户地址")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteAddress(@RequestParam("id") Long id) {
        addressService.deleteAddress(getLoginUserId(), id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户地址")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MemberAddressRespDTO> getAddress(@RequestParam("id") Long id) {
        MemberAddressDO address = addressService.getAddress(getLoginUserId(), id);
        return success(toRespDTO(address));
    }

    @GetMapping("/get-default")
    @Operation(summary = "获得默认的用户地址")
    public CommonResult<MemberAddressRespDTO> getDefaultAddress() {
        MemberAddressDO address = addressService.getDefaultAddress(getLoginUserId());
        return success(toRespDTO(address));
    }

    @PostMapping("/save-dine")
    @Operation(summary = "保存点餐地址")
    public CommonResult<Boolean> saveDineAddress(@Valid @RequestBody AppAddressSaveReqVO reqVO) {
        addressService.saveDineAddress(getLoginUserId(), reqVO);
        return success(true);
    }

    @GetMapping("/get-dine")
    @Operation(summary = "获得用户点餐地址")
    public CommonResult<MemberAddressRespDTO> getDineAddress() {
        MemberAddressDO address = addressService.getDineAddress(getLoginUserId());
        return success(toRespDTO(address));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户地址列表")
    public CommonResult<List<MemberAddressRespDTO>> getAddressList() {
        List<MemberAddressDO> list = addressService.getAddressList(getLoginUserId());
        if (CollUtil.isEmpty(list)) {
            return success(Collections.emptyList());
        }
        List<MemberAddressRespDTO> results = new ArrayList<>(list.size());
        for (MemberAddressDO item : list) {
            results.add(toRespDTO(item));
        }
        return success(results);
    }

    private MemberAddressRespDTO toRespDTO(MemberAddressDO bean) {
        if (bean == null) {
            return null;
        }
        String countyCode = RegionUtils.convertRegionIdToCode(bean.getAreaId());
        MemberAddressRespDTO respDTO = BeanUtils.toBean(bean, MemberAddressRespDTO.class);
        RegionUtils.autoAssignCounty(RegionDataSet.ONLY_LATEST, countyCode, respDTO);
        return respDTO;
    }

}
